{ "cells": [ { "cell_type": "markdown", "id": "44ca09e5", "metadata": {}, "source": [ "# Programming Techniques — Laboratory\n", "## Input, Type Conversion and Strings\n", "\n", "This laboratory follows the order used in class, but the exercises deliberately **combine several concepts**.\n", "\n", "### Topics\n", "- `input()`\n", "- type conversion with `int()`, `float()` and `str()`\n", "- `int()` with a **base** (binary, hexadecimal, ...)\n", "- common conversion errors\n", "- `len()` with strings\n", "- character indexes\n", "- slicing, including negative steps\n", "- escape sequences\n", "- functions vs. methods\n", "- string methods: `.lower()`, `.upper()`, `.replace()`\n", "- f-strings\n", "\n", "### Working style\n", "1. **Guided exercise** — follow the instructor hands-on.\n", "2. **Laboratory exercises** — work semi-independently; ask questions and discuss your choices.\n", "3. **Independent review** — more complete exercises for consolidation and homework.\n", "\n", "> In this notebook we use strings as sequences. We do **not** use lists or loops yet." ] }, { "cell_type": "markdown", "id": "677b9077", "metadata": {}, "source": [ "---\n", "# Part A — Guided Exercise\n", "## 1. Conference registration receipt\n", "\n", "### Theory recall\n", "\n", "`input()` always returns a **string**:\n", "\n", "```python\n", "name = input(\"Participant name: \")\n", "```\n", "\n", "When a string represents a number, it can be converted:\n", "\n", "```python\n", "tickets = int(\"3\") # 3\n", "price = float(\"12.50\") # 12.5\n", "text = str(42) # \"42\"\n", "```\n", "\n", "### `int()` and number bases\n", "\n", "`int()` can also interpret a string using a specified **base**:\n", "\n", "```python\n", "int(\"1010\", 2) # 10 binary → integer\n", "int(\"1A\", 16) # 26 hexadecimal → integer\n", "int(\"25\", 10) # 25 decimal → integer\n", "```\n", "\n", "The second argument tells Python how to interpret the digits.\n", "\n", "Invalid digits for the selected base cause a `ValueError`:\n", "\n", "```python\n", "int(\"102\", 2) # ValueError: 2 is not a binary digit\n", "int(\"1G\", 16) # ValueError: G is not a hexadecimal digit\n", "int(\"3.14\") # ValueError: not an integer representation\n", "```\n", "\n", "For decimal numbers with a fractional part, use `float()`:\n", "\n", "```python\n", "float(\"3.14\") # 3.14\n", "```\n", "\n", "### Task\n", "\n", "Build a small registration receipt.\n", "\n", "Ask the user for:\n", "- participant name;\n", "- number of tickets;\n", "- price per ticket;\n", "- a short **binary registration code**.\n", "\n", "Then:\n", "1. convert the numeric inputs;\n", "2. convert the binary code to an integer using `int(..., 2)`;\n", "3. calculate the total cost;\n", "4. print a compact receipt using an **f-string**.\n", "\n", "Example:\n", "\n", "```text\n", "Participant name: Sofia\n", "Number of tickets: 3\n", "Price per ticket: 12.50\n", "Binary registration code: 10101\n", "\n", "Sofia bought 3 tickets.\n", "Total: €37.50\n", "Registration code: 21\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "b59af968", "metadata": {}, "outputs": [], "source": [ "name = input(\"Participant name: \")\n", "\n", "# TODO: read the remaining values\n", "# TODO: convert them to the appropriate types\n", "# TODO: convert the binary registration code using int(binary_text, 2)\n", "# TODO: calculate the total\n", "# TODO: print the receipt with f-strings" ] }, { "cell_type": "markdown", "id": "abc6d2f5", "metadata": {}, "source": [ "### Quick discussion\n", "- What type does `input()` return?\n", "- Why do we need `int()` or `float()` before arithmetic?\n", "- What does the `2` mean in `int(binary_code, 2)`?\n", "- What would happen with `int(\"102\", 2)`?\n", "- What does `{total:.2f}` do inside the f-string?" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }